//
//  DetailViewController.m
//  WDFans
//
//  Created by Zhihong Yuan on 2/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DetailViewController.h"
#import "WDNews.h"
#import "ZHDataService.h"
#import "ActivityViewManager.h"

@implementation DetailViewController

@synthesize currentNews;
@synthesize myWebView;

#pragma mark - AbstractTabViewController methods
- (IBAction)rightItemClicked
{
//    if([[ZHDataService sharedService].weibo isUserLoggedin])
//    {
//        [ZHDataService sharedService].weibo.delegate = self;
//        [[ZHDataService sharedService].weibo showSendViewWithWeiboText:@"我正在试用西部数据俱乐部！" andImage:[UIImage imageNamed:@"icon.png"] andDelegate:self];
//    }
//    else
//    {
//        [ZHDataService sharedService].weibo.delegate = self;
//        [[ZHDataService sharedService].weibo startAuthorize];
//    }
}




#pragma mark - View lifecycle
- (void)dealloc
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [myWebView release];
    [currentNews release];
    [super dealloc];
}

- (id)initWithNews:(WDNews *)curNews
{
    self = [super initWithNibName:@"DetailViewController" bundle:nil];
    if (self) {
        // Custom initialization
        self.currentNews = curNews;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.title = self.currentNews.newsTitle;
    self.title = @"热点咨询";
    myWebView.backgroundColor = [UIColor clearColor];
    [self setBackgroundImageViewForTabView];
    [self addNaviBarRightItemWithTitle:@"分享微博"];
    if(SYSTEW_VERSION_LESS_THAN(@"5.0"))
    {
        UIScrollView *scrollView = (UIScrollView *)[[self.myWebView subviews] objectAtIndex:0];
        if(scrollView)
        {
            scrollView.bounces = NO;
        }
    }
    else
        self.myWebView.scrollView.bounces = NO;
    
    self.currentNews.newsContent = [NSString stringWithFormat:@"<P align=center><font size=5>%@</font></P><P align=center>%@</P>%@",self.currentNews.newsTitle,self.currentNews.newsDate,self.currentNews.newsContent];
    
    [self.myWebView loadHTMLString:self.currentNews.newsContent baseURL:nil];
    self.myWebView.hidden = YES;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.currentNews = nil;
    self.myWebView = nil;
}

#pragma mark - UIWebviewDelegate methods
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSString *font = (NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:kFontKey];
    if([font isEqualToString:@"大"])
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'"];
    else if([font isEqualToString:@"中"])
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '90%'"];
    else
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '80%'"];
    webView.hidden = NO;
}
@end
